import 'dart:async';

import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_application_1/components/article_cart.dart';
import 'package:flutter_application_1/components/song_card.dart';
import 'package:flutter_application_1/components/video_cart.dart';
import 'package:flutter_application_1/models/recommend_interface.dart';
import 'package:flutter_application_1/models/video_model.dart';

import '../../components/singer_card.dart';
import '../../models/article_model.dart';
import '../../models/song_model.dart';
import '../../models/user_model.dart';
import '../../services/recommend_service.dart';

class RecommendPage extends StatefulWidget {
  const RecommendPage({Key? key}) : super(key: key);

  @override
  _RecommendPageState createState() => _RecommendPageState();
}

class _RecommendPageState extends State<RecommendPage>
    with AutomaticKeepAliveClientMixin {
  late EasyRefreshController _controller;
  List<RecommendInterface> _recommendList = [];
  int page = 1;
  int limit = 10;
  bool hasMore = true;
  bool error = false;
  String errorMsg = '';
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = EasyRefreshController(
        controlFinishLoad: true, controlFinishRefresh: true);
    _getRecommends();
  }

  Future _getRecommends({bool push = false}) async {
    List<dynamic> result = await RecommendService.getRecommends(page: page);
    // RecommendInterface recommendListModel;
    List<RecommendInterface> recommendListModels = result.map((e) {
      if (e["contentType"] == "article") {
        return ArticleItem.fromJson(e);
      } else if (e["contentType"] == "song") {
        return SongItem.fromJson(e);
      } else if (e["contentType"] == "video") {
        return VideoItem.fromJson(e);
      } else {
        return UserItem.fromJson(e);
      }
    }).toList();
    setState(() {
      //hasMore
      page++;
      if (push) {
        _recommendList.addAll(recommendListModels);
      } else {
        _recommendList = recommendListModels;
      }
    });
  }

  FutureOr _onRefresh() async {
    page = 1;
    await _getRecommends();
    _controller.finishRefresh();
    _controller.resetFooter();
  }

  FutureOr _onLoad() async {
    await _getRecommends(push: true);
    _controller.finishLoad();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return EasyRefresh(
      controller: _controller,
      header: ClassicHeader(),
      footer: ClassicFooter(),
      onRefresh: _onRefresh,
      onLoad: _onLoad,
      child: ListView.builder(
        itemCount: _recommendList.length,
        itemBuilder: (BuildContext context, int index) {
          RecommendInterface widgetItem = _recommendList[index];
          return Container(
            margin: EdgeInsets.symmetric(vertical: 8, horizontal: 8),
            child: getCard(widgetItem),
          );
        },
      ),
    );
  }

  Widget getCard(RecommendInterface widgetItem) {
    if (widgetItem is SongItem) {
      return SongCard(songItem: widgetItem);
    } else if (widgetItem is UserItem) {
      return SingerCard(userItem: widgetItem);
    } else if (widgetItem is ArticleItem) {
      return ArticleCart(articleItem: widgetItem);
    } else if (widgetItem is VideoItem) {
      return VideoCart(videoItem: widgetItem);
    } else {
      return Text("");
    }
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
